#!/bin/sh

ifinstall(){
    eipath=`whereis $1`
    result=1
    if [ "$eipath" == "$1:" ]
    then
        result=0
    fi
    return $result
}

if_service_start(){
#    runstatus=`systemctl status $1 | grep "active (running)"`
    runstatus=`systemctl status shadowsocks | grep "active (running)" | grep -v "grep"`
    result=1
    if [ "$runstatus" == "" ]; then
        result=0
    fi
    return $result
}


install_shadowsocks(){
    ifinstall "easy_install"
    if [ $? -eq 1 ]; then
	echo "had installed python setuptools"
    else
        yum install python-setuptools -y
    fi
    ifinstall "pip"
    if [ $? -eq 1 ]; then
	echo "had installed pip"
    else
        easy_install pip
    fi
    ifinstall "sslocal"
    if [ $? -eq 1 ]; then
	echo "had installed shadowsocks"
    else
        pip install shadowsocks
    fi
}

create_ss_client_cfg(){
    mkdir -p /etc/shadowsocks
    cat >/etc/shadowsocks/shadowsocks.json <<EOF
{
  "server":"$1",
  "server_port":443,
  "local_address": "127.0.0.1",
  "local_port":1080,
  "password":"$2",
  "timeout":300,
  "method":"aes-256-cfb",
  "workers": 1
}
EOF
    cat >/etc/systemd/system/shadowsocks.service <<EOF
[Unit]
Description=Shadowsocks
[Service]
TimeoutStartSec=0
ExecStart=/usr/bin/sslocal -c /etc/shadowsocks/shadowsocks.json
[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload 
}

start_ss_client(){
    if_service_start "shadowsocks"
    if [ $? -eq 1 ]; then
        systemctl restart shadowsocks
    else
        systemctl start shadowsocks
    fi
    if_service_start "shadowsocks"
    if [ $? -eq 1 ]; then
        echo "shadowsocks is runing"
    else
        echo "shadowsocks run fail"
    fi
}

build_privoxy(){
    ifinstall "privoxy"
    if [ $? -eq 1 ]; then
        echo "had install privoxy"
        return
    fi

   ifinstall "wget"
    if [ $? -eq 0 ]; then
        yum install wget -y
    fi
    ifinstall "gcc"
    if [ $? -eq 0 ]; then
        yum install gcc -y
    fi
    ifinstall "autoconf"
    if [ $? -eq 0 ]; then
        yum install autoconf -y
    fi

    useradd privoxy

    mkdir -p /opt/privoxy && cd /opt/privoxgcc y
    wget https://gitee.com/gaoguangting/rc/raw/master/privoxy-3.0.26-stable-src.tar.gz
    tar -zxvf privoxy-3.0.26-stable-src.tar.gz
    cd privoxy-3.0.26-stable
    autoheader && autoconf
    ./configure
    make && make install

    echo "forward-socks5t    /    127.0.0.1:1080 ." >> /usr/local/etc/privoxy/config
}

start_privoxy(){
    privoxy_pid=`ps aux | grep privoxy | grep -v "grep" | awk '{print $2}'`
    if [ "$privoxy_pid" == "" ]; then
        privoxy --user privoxy /usr/local/etc/privoxy/config
    else
        echo "privoxy is running"
    fi
}

stop_privoxy(){
    privoxy_pid=`ps aux | grep privoxy | grep -v "grep" | awk '{print $2}'`
    echo $privoxy_pid
    if [ "$privoxy_pid" != "" ]; then
        kill -s 9 $privoxy_pid
    fi
}

set_proxy_env(){
    hpunset=`grep -nr '^[ \t]*#[ \t]*export[ \t].*http_proxy[ \n]*=' /etc/profile | awk -F ':' '{print $1}'`
    if [ "$hpunset" == "" ]; then
        hpset=`grep -nr '^[ \t]*export[ \t].*http_proxy[ \n]*=' /etc/profile | awk -F ':' '{print $1}'`
	if [ "$hpset" == "" ]; then
            echo 'export all_proxy=http://127.0.0.1:8118' >> /etc/profile
            echo 'export ftp_proxy=http://127.0.0.1:8118' >> /etc/profile
            echo 'export http_proxy=http://127.0.0.1:8118' >> /etc/profile
            echo 'export https_proxy=http://127.0.0.1:8118' >> /etc/profile
            echo 'export no_proxy=localhost,172.16.0.0/16,192.168.0.0/16.,127.0.0.1,10.10.0.0/16' >> /etc/profile
        fi
    else
        sed -i 's/^[[:blank:]]*#[[:blank:]]*export[ \t].*all_proxy[[:blank:]]*=/export all_proxy=/' /etc/profile
        sed -i 's/^[[:blank:]]*#[[:blank:]]*export[ \t].*ftp_proxy[[:blank:]]*=/export ftp_proxy=/' /etc/profile
        sed -i 's/^[[:blank:]]*#[[:blank:]]*export[ \t].*http_proxy[[:blank:]]*=/export http_proxy=/' /etc/profile
        sed -i 's/^[[:blank:]]*#[[:blank:]]*export[ \t].*https_proxy[[:blank:]]*=/export https_proxy=/' /etc/profile
        sed -i 's/^[[:blank:]]*#[[:blank:]]*export[ \t].*no_proxy[[:blank:]]*=/export no_proxy=/' /etc/profile
    fi

    source /etc/profile
}

unset_proxy_env(){
    hpset=`grep -nr '^[ \t]*export[ \t].*http_proxy[ \n]*=' /etc/profile | awk -F ':' '{print $1}'`
    #hpunset=`grep -nr '^[ \t]*#[ \t]*export[ \t].*http_proxy[ \n]*=' /etc/profile | awk -F ':' '{print $1}'`
    if [ "$hpset" != "" ]; then
        sed -i 's/^[[:blank:]]*export[ \t].*all_proxy[[:blank:]]*=/#export all_proxy=/' /etc/profile
        sed -i 's/^[[:blank:]]*export[ \t].*ftp_proxy[[:blank:]]*=/#export ftp_proxy=/' /etc/profile
        sed -i 's/^[[:blank:]]*export[ \t].*http_proxy[[:blank:]]*=/#export http_proxy=/' /etc/profile
        sed -i 's/^[[:blank:]]*export[ \t].*https_proxy[[:blank:]]*=/#export https_proxy=/' /etc/profile
        sed -i 's/^[[:blank:]]*export[ \t].*no_proxy[[:blank:]]*=/#export no_proxy=/' /etc/profile
    fi
    source /etc/profile
    export all_proxy=""
    export ftp_proxy=""
    export http_proxy=""
    export https_proxy=""
    export no_proxy=""
}

case $1 in
    vpnstart | vs)
        if [ $# -lt 3 ]; then
            echo "$0 $1 <shadowsocks server> <shadowsocks password>"
            exit 0
        fi

        install_shadowsocks
        create_ss_client_cfg $2 $3
        start_ss_client
        build_privoxy
        start_privoxy
        set_proxy_env
    ;;
    vpnstop)
        echo "stoping vpn"    
        systemctl stop shadowsocks
        stop_privoxy
        unset_proxy_env
    ;;
    *)
        echo "$0 [vpnstart/vpnstop]"
    ;;
esac
